#-*-coding:utf-8-*-
import pyttsx3

'''
输出
'''
def ifPrint(flag,msg):
    if flag == True:
        print(msg)

FLAG = False

'''
创建pyttsx3 engine实例
'''
def initPyttsx3():
    # 创建对象
    rob = pyttsx3.init()
    # 获取当前语音速率
    rate = rob.getProperty('rate')
    # 设置新的语音速率
    rob.setProperty('rate', 175)
    ifPrint(FLAG,f'语音速率：{rate}')
    # 获取当前语音音量
    volume = rob.getProperty('volume')
    # 设置新的语音音量，音量最小为 0，最大为 1
    rob.setProperty('volume', 1.0)
    ifPrint(FLAG,f'语音音量：{volume}')
    # 获取当前语音声音的详细信息
    voices = rob.getProperty('voices')
    #for _v in voices:
    #    ifPrint(FLAG,f' id:{_v.id} \n name:{_v.name}')
    # 设置当前语音声音为，当前声音不能读中文
    #rob.setProperty('voice', voices[1].id)
    # 设置当前语音声音为，当前声音可以读中文
    rob.setProperty('voice', voices[0].id)
    #print(f'语音声音详细信息：{voices}')
    # 获取当前语音声音
    voice = rob.getProperty('voice')
    ifPrint(FLAG,f'语音声音：{voice}')
    return rob

'''
处理文本
'''
def processTxt(rob,txt,audioOut):
    rob.save_to_file(txt,audioOut)
    rob.runAndWait()

#def deletePyttsx3(rob):
#    rob.stop()

if __name__ == '__main__':
    msg = ''''''
    with open('msg.txt','r',encoding='utf-8') as file:
        msg = file.read()
        #print(msg)
        #exit()
    rob = initPyttsx3()
    processTxt(rob,msg,'msg.mp3')
    rob.stop()
